
import java.io.InputStream;

/**
 * @author Pheenix
 * Lädt Daten aus Dateien in Variablen.
 * Wird in system.java einmalig initalisiert.
 */
public class InputStreams {

    public static InputStream[] sound, map;

    InputStreams() {
        sound = new InputStream[2];
        sound[0] = getClass().getResourceAsStream("music_RightHere.mid");
        sound[1] = getClass().getResourceAsStream("music_Crawling.mid");

        map = new InputStream[3];
        map[0] = getClass().getResourceAsStream("/mapsLeicht.txt");
        map[1] = getClass().getResourceAsStream("/mapsMittel.txt");
        map[2] = getClass().getResourceAsStream("/mapsSchwer.txt");

        level.mapsLeicht = load(map[0]);
        level.mapsMittel = load(map[1]);
        level.mapsSchwer = load(map[2]);

    }
    //------------------------------Textloader----------------------------------

    /**
     * Wandelt .txt-Dateien in 2-dimensionale MapArrays um.
     * @param is
     * @return int[][]
     */
    private int[][] load(InputStream is) {
        StringBuffer sb = new StringBuffer();
        try {
            int c;
            while ((c = is.read()) != -1) {
                sb.append((char) c);
            }
            is.close();
        } catch (Exception e) {
        }

        int mapLaenge = 0;
        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) == '!') {
                mapLaenge++;
            }
        }

        int mapListe[][] = new int[mapLaenge][];
        int mapNr = 0, charP = 0;
        while (mapNr < mapLaenge) {
            //map Länge herrausfinden
            int laenge = 0;
            for (int z = charP; sb.charAt(z) != '!'; z++) {
                if (system.isInt(sb.charAt(z))) {
                    laenge++;
                }
            }
            //Liste erstellen
            int[] liste = new int[laenge];
            for (int i = 0; i < laenge; i++) {
                while (sb.charAt(charP) != ',' && sb.charAt(charP) != '!') {
                    if (!system.isInt(sb.charAt(charP))) {
                        charP++;
                        continue;
                    }
                    liste[i] = Integer.parseInt(String.valueOf(sb.charAt(charP)));
                    charP++;
                }
                charP++;
            }
            mapListe[mapNr] = liste;
            mapNr++;
        }
        return mapListe;
    }
}
